package _stack_queue

import org.junit.Assert
import org.junit.Test
import java.util.*

/**
 * https://programmercarl.com/0232.%E7%94%A8%E6%A0%88%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97.html
 * https://leetcode.cn/problems/implement-queue-using-stacks/description/
 *
 * 232. 用栈实现队列
 *
 * MyQueue myQueue = new MyQueue();
 * myQueue.push(1); // queue is: [1]
 * myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
 * myQueue.peek(); // return 1
 * myQueue.pop(); // return 1, queue is [2]
 * myQueue.empty(); // return false
 *
 */
class leetcode_232 {
    @Test
    fun test_1() {
        val myQueue: MyQueue = MyQueue();
        myQueue.push(1)
        myQueue.push(2)
        Assert.assertEquals(1, myQueue.peek())
        Assert.assertEquals(1, myQueue.pop())
        Assert.assertEquals(false, myQueue.empty())
    }


    /**
     * Your MyQueue object will be instantiated and called as such:
     * var obj = MyQueue()
     * obj.push(x)
     * var param_2 = obj.pop()
     * var param_3 = obj.peek()
     * var param_4 = obj.empty()
     */
    private class MyQueue() {
        /**
        队列 <= 3 2 1
        Stack in  <= 3  2 1
        Stack out  1 2 3  =>
         */
        val stackIn: Stack<Int> = Stack<Int>()
        val stackOut: Stack<Int> = Stack<Int>()

        fun push(x: Int) {
            stackIn.push(x)
        }

        fun pop(): Int {
            // out is empty, move in -> out
            if (stackOut.isEmpty()) {
                while (!stackIn.isEmpty()) {
                    stackOut.push(stackIn.pop())
                }
            }
            // pop
            return stackOut.pop()
        }

        fun peek(): Int {
            val num = pop()
            return stackOut.push(num)
        }

        fun empty(): Boolean {
            return stackOut.isEmpty() && stackIn.isEmpty()
        }

    }
}